package com.wimcorp.magic.abilities;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.wimcorp.magic.abilities.trigger.AbilityTrigger;
import com.wimcorp.magic.board.PlayingDeck;
import com.wimcorp.magic.cards.PlayingCard;
import com.wimcorp.magic.input.Location;

/**
 * example {amount : 2, chosen_destination : 'HAND',
 * un_chosen_destination : 'BOTTOM_OF_DECK'}
 * 
 * 
 * chosen_destination : HAND,BOTTOM_OF_DECK
 * 
 * un_chosen_destination : HAND,BOTTOM_OF_DECK
 */
@Entity
@Table(name = "revealdeckandchoose", schema = "abilities")
public class RevealDeckAndChoose extends Ability {

	@Transient
	List<PlayingCard> cards = new ArrayList<>();

	@Enumerated(EnumType.STRING)
	private Location chosenDestination;
	
	@Enumerated(EnumType.STRING)
	private Location unChosenDestination;
	
	private int amount;
	
	protected RevealDeckAndChoose() {
	}

	public RevealDeckAndChoose(AbilityTrigger trigger) {
		super(trigger);
	}
	
	public RevealDeckAndChoose(RevealDeckAndChoose ability) {
		super(ability);
		this.chosenDestination = ability.getChosenDestination();
		this.unChosenDestination = ability.getUnChosenDestination();
		this.amount = ability.getAmount();
	}

	@Override
	public long getInput() {
		PlayingDeck deck = getPlayer().getDeck();
		try {
			getPlayer().getUserInterface().message(getDescription());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		long[] options = new long[amount];
		for (int i = 0; i < amount; i++) {
			cards.add(deck.getTopCard());
		}
		for (int i = 0; i < amount; i++) {
			try {
				getPlayer().getUserInterface().message(cards.get(0).toString());
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			options[i] = cards.get(0).getIdentifier();
		}
		Long[] result = new Long[amount];
		try {
			result = getPlayer().getUserInterface().order(
					"Choose card for " + chosenDestination.getName() + " : ", options);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result[0];

	}

	@Override
	protected void executeAbility() {
		for(PlayingCard card : cards){
			if(card.getIdentifier() == getInput()){
				moveCard(card,chosenDestination);
			}else{
				moveCard(card,unChosenDestination);
			}
		}
	}

	private void moveCard( PlayingCard card,Location location) {
		switch (location) {
		case BOTTOM_DECK:
			card.unsummon();
			getPlayer().getDeck().addOnBottom(card);
			break;
		case EXILE:
			card.exile();
			break;
		case GRAVEYARD:
			card.graveyard();
			break;
		case HAND:
			card.unsummon();
			getPlayer().getHand().add(card);
			break;
		case TOP_DECK:
			card.unsummon();
			getPlayer().getDeck().addOnTop(card);
			break;
		}
	}

	public Location getChosenDestination() {
		return chosenDestination;
	}

	public Location getUnChosenDestination() {
		return unChosenDestination;
	}

	public int getAmount() {
		return amount;
	}

	@Override
	public RevealDeckAndChoose clone() {
		// TODO Auto-generated method stub
		return new RevealDeckAndChoose(this);
	}

	public void setChosenDestination(Location chosenDestination) {
		this.chosenDestination = chosenDestination;
	}

	public void setUnChosenDestination(Location unChosenDestination) {
		this.unChosenDestination = unChosenDestination;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}
	
	

}
